package org.jeecg.common.util;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.jeecg.common.RestBaseErrorEnum;

@Data
public class Rest<T> {

    @ApiModelProperty(value = "是否成功(true成功，false失败)")
    private boolean success = true;
    @ApiModelProperty(value = "返回对象")
    private T data;
    @ApiModelProperty(value = "错误编号（0为正确，-1出错）")
    private String errCode = RestBaseErrorEnum.SUCCESS.getResultCode();
    @ApiModelProperty(value = "错误信息")
    private String message = RestBaseErrorEnum.SUCCESS.getResultMsg();

    public Rest() {
        super();
    }

    public Rest(boolean success) {
        super();
        this.success = success;
    }

    public Rest(T data) {
        super();
        this.data = data;
        this.success = true;
    }

    public Rest(boolean success, String message) {
        super();
        this.success = success;
        this.message = message;
    }

    public Rest(boolean success, T data, String errCode, String message) {
        super();
        this.success = success;
        this.data = data;
        this.errCode = errCode;
        this.message = message;
    }

    public Rest<T> error500(String message) {
        this.message = message;
        this.errCode = RestBaseErrorEnum.INTERNAL_SERVER_ERROR.getResultCode();
        this.success = false;
        return this;
    }

    public Rest<T> success(String message) {
        this.message = message;
        this.success = true;
        return this;
    }


    public static Rest<Object> ok() {
        Rest<Object> r = new Rest<Object>();
        r.setSuccess(true);
        r.setMessage("成功");
        return r;
    }

    public static Rest<Object> ok(String msg) {
        Rest<Object> r = new Rest<Object>();
        r.setSuccess(true);
        r.setMessage(msg);
        return r;
    }

    public static <T> Rest<T> ok(T data) {
        Rest<T> r = new Rest<>();
        r.setSuccess(true);
        r.setData(data);
        return r;
    }

    public static Rest<Object> error(String msg) {
        return error(RestBaseErrorEnum.INTERNAL_SERVER_ERROR.getResultCode(), msg);
    }

    public static Rest<Object> error(String code, String msg) {
        Rest<Object> r = new Rest<Object>();
        r.setErrCode(code);
        r.setMessage(msg);
        r.setSuccess(false);
        return r;
    }

    /**
     * 无权限访问返回结果
     */
    public static Rest<Object> noauth(String msg) {
        return error(RestBaseErrorEnum.NO_AUTHZ.getResultCode(), msg);
    }

}
